package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Configuration
@RestControllerAdvice
public class ExceptionCatch {

    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e){
        //打印错误信息
        e.printStackTrace();
        //打印日志
        log.error("出错信息 :{}",e);


        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器忙。。。");
    }

    //拦截自定义异常处理
    @ExceptionHandler(CustomException.class)
    public ResponseResult custException(CustomException e) {
        e.printStackTrace();
        log.error("CustomException e:{}", e);
        AppHttpCodeEnum codeEnum = e.getAppHttpCodeEnum();
        return ResponseResult.errorResult(codeEnum);
    }

}
